<?php 
// +----------------------------------------------------------------------
// | Item        短信控制器
// +----------------------------------------------------------------------
// | Copyright
// +----------------------------------------------------------------------
// | Versions
// +----------------------------------------------------------------------
// | Author       YanHao
// +----------------------------------------------------------------------
// | Date         2017-07-12
// +----------------------------------------------------------------------

namespace app\admin\controller;

class SnsMsg extends Base 
{
    /** 默认分页 */
   const PAGE_SIZE = 10;
    /**
     * header 
     * @var string
     */
    protected $header = '短信管理';

    /**
     * 短信记录列表
     * @Author  YanHao
     * @Date    2017-07-12
     * @version [version]
     * @return  [type]     [description]
     */
    public function msgLists() 
    {
        $config = $where = [];
        $pageSize = self::PAGE_SIZE;
        if ($this->request->isGet() && $this->request->has('query')) {
            $get = $this->request->get();
            if (isset($get['status']) && $get['status'] !=-1) {
                $config['query']['query'] = 1;
                $where['status'] = $config['query']['status'] = $get['status'];
            }
            if (isset($get['phone']) && $get['phone']){
                $config['query']['query'] = 1;
                $where['phone'] = ['like', '%'.$get['phone']. '%'];
                $config['query']['phone'] = $get['phone'];
            }
            if (isset($get['id']) && $get['id']){
                $config['query']['query'] = 1;
                $where['id'] = ['like', '%'.$get['id']. '%'];
                $config['query']['id'] = $get['id'];
            }
            $pageSize = isset($get['pageSize']) ? $get['pageSize'] : self::PAGE_SIZE;
            $config['query']['pageSize'] = $get['pageSize'];
        }
        $data = parent::model('SnsLog')->getAllMsg($where, $pageSize, $config);
        $this->view->desc = '短信记录';
        return $this->fetch('msgLists', [
            'data'     => $data,
            'pageSize' => $pageSize, 
            'dataNum'  => count(parent::model('SnsLog')->getAll())
          ]);
    }
    //短信再次发送
    public function msgResend()
    {
        $id = $this->request->post('id');
        return parent::model('SnsLog')->resendMsg($id);
    }
    /**
     * 短信模板列表
     * @Author  YanHao
     * @Date    2017-07-12
     * @version [version]
     * @return  [type]     [description]
     */
    public function msgTemplateLists()
    {
        $config = $where = [];
        $pageSize = self::PAGE_SIZE;
        if ($this->request->isGet() && $this->request->has('query')) {
            $get = $this->request->get();
            if (isset($get['status']) && $get['status'] !=-1) {
                $config['query']['query'] = 1;
                $where['status'] = $config['query']['status'] = $get['status'];
            }
            if (isset($get['name']) && $get['name']){
                $config['query']['query'] = 1;
                $where['name'] = ['like', '%'.$get['name']. '%'];
                $config['query']['name'] = $get['name'];
            }
            if (isset($get['id']) && $get['id']){
                $config['query']['query'] = 1;
                $where['id'] = ['like', '%'.$get['id']. '%'];
                $config['query']['id'] = $get['id'];
            }
            $pageSize = isset($get['pageSize']) ? $get['pageSize'] : self::PAGE_SIZE;
            $config['query']['pageSize'] = $get['pageSize'];
        }
        $data = parent::model('SnsTemplate')->getAllTemplate($where, $pageSize, $config);
        $this->view->desc = '短信模板';
        return $this->fetch('msgTemplateLists', [
                'data'     => $data,
                'pageSize' => $pageSize,
                'dataNum'  => count(parent::model('SnsTemplate')->getAll())
            ]);
    }
    /**
     * 新增、编辑短信模板
     * @Author  YanHao
     * @Date    2017-07-12
     * @version [version]
     */
    public function addMsgTemplate()
    {
        $id = $this->request->param('id');
        if ($id) {
            $this->view->desc= '编辑模板';
            $tempData = parent::model('SnsTemplate')->getOne($id, true);
        } else {
            $this->view->desc = '新增模板';
            $tempData = [];
        }
        return $this->fetch('addMsgTemplate', ['tempData' => $tempData]);
    }
    /**
     * 添加、修改短信模板 数据 提交
     * @Author  YanHao
     * @Date    2017-07-12
     * @version [version]
     */
    public function sendMsgTemplate()
    {
        $post = $this->request->post();
        return parent::model('SnsTemplate')->createTemplate($post);
    }
    /**
     * 设置模板启用状态
     * @Author  YanHao
     * @Date    2017-07-12
     * @version [version]
     */
    public function setTempStatus()
    {
        $post = $this->request->post();
        return parent::model('SnsTemplate')->changeStatus($post['status'], $post['id']);
    }
    /**
     * 删除模板
     * @Author  YanHao
     * @Date    2017-07-13
     * @version [version]
     * @return  [type]     [description]
     */
    public function deleteTemplate()
    {
        $id = $this->request->post('id');
        $handle = parent::model('SnsTemplate')->deleteOne($id);
        return $handle ? ['code' => 1, 'msg' =>'删除成功'] : ['code' => 0, 'msg' =>'删除失败'];
    }


    
}